<template>
  <div class="section-wrap" style="height:100%;padding-top: 40px" @click="openRightIndex=-1">
    <div class="add_btn" @click="flag=true"></div>
    <!-- 头部 -->

    <div class="headerBox">
        <div class="header">
            <x-icon type="ios-arrow-left" size="" class="icon-white" @click="goBack"></x-icon>
            量化考核计划
        </div>
    </div>
     <vue-better-scroll
      class="wrapper"
      ref="scrollqq"
      :scrollbar="scrollbarObj"
      :pullDownRefresh="pullDownRefreshObj"
      :pullUpLoad="pullUpLoadObj"
      :startY='parseInt(startY)'
      @pullingUp="onPullingUp"
    >
    <div class="form-section" style="margin-top: 40px;">
          <div class="form-item form-item-style1" @click="getCompany">
              <div class="label-title">
                <label>被考核单位</label>
              </div>
              <div class="input_div">
                <input type="text"
                       v-model="detailData.beDepartName"
                       :disabled="isDisabled">
                <input type="hidden" v-model="params.beDepartId"/>
                <x-icon type="ios-arrow-right" size="22" class="icon-right" ></x-icon>
              </div>
            </div>
        <div class="form-item form-item-style1">
          <div class="label-title">
            <label>查询开始时间</label>
          </div>
          <group>
            <datetime v-model="startDate" clear-text="清除" @on-clear="clearStartDate" format="YYYY-MM-DD"></datetime>
          </group>
        </div>
        <div class="form-item form-item-style1">
          <div class="label-title">
            <label>查询结束时间</label>
          </div>
          <group>
            <datetime v-model="endDate" clear-text="清除"  @on-clear="clearEndDate" format="YYYY-MM-DD"></datetime>
          </group>
        </div>
        <p class="search-btn" @click="search">查询</p>
        <div class="info_div">
          <div class="form-item" style="height: auto">
            <div v-for="(value,key) in resultTotal"><span>{{key}}：{{value}}次</span></div>
           <!-- <div><span>线路工程量化考核：22次</span><span>变电站工程量化考核：6次</span></div>-->
          </div>
        </div>
        <div class="project_div">
          <ul class="project_list">
            <li v-for="(items,index) in resultlist" :key="index">
              <v-touch v-on:swipeleft="onSwipeLeft(index)" tag="div" style="touch-action:pan-y !important;">
              <div @click="nextStep(items)">
                <h4>{{items.beDepartName}} <span style="font-size: 14px;padding-left: 10px">{{items.checkNo}}</span></h4>
                <p><span>{{items.classifyTwoName}}</span><span class="project_time">{{items.checkDate.substring(0,10)}}</span></p>
                <p class="project_status"><strong class="project_status1" v-if="items.status==0">【未提交】</strong>
                <p class="project_status">
                  <strong class="project_status2" v-if="items.status==1">【已提交】</strong>
                  <strong class="project_status2" v-if="items.status==2">【审批通过】</strong>
                  <strong class="project_status1" v-if="items.status==3">【审批不通过】</strong>
                </p>
                <x-icon type="ios-arrow-right" size="22" class="icon-right"></x-icon>
              </div>
              <div style="height: 105px;width: 100px;position: absolute;top:0;right: 0;cursor: pointer" v-if="openRightIndex==index">
                <p style="width:50px;text-align:center;height: 105px;line-height: 105px;background-color: red;color: #fff;float: left" @click="del(items,index)">删除</p>
                <p style="width:50px;text-align:center;height: 105px;line-height: 105px;background-color: #ccc;color: #fff;float: right" @click="openRightIndex=-1">取消</p>
            </div>
              </v-touch>
            </li>
          </ul>
        </div>
      </div>


    </vue-better-scroll>
    <div class="addPlan_select" @click="flag=!flag" v-if="flag">
      <ul class="planSel_list">
        <li v-for="(type,index) in tableType" :key="index" @click="gotoAddPlan(type)">{{type.quantizeName}}</li>
        <!--<li @click="$router.push('./addPlan')">线路工程量化考核</li>
        <li @click="$router.push('./addPlan')">变电站工程量化考核</li>-->
      </ul>
    </div>
    <!--classValue 显示隐藏   data树结构  nodeClicK节点点击返回值  name显示的字段  people -->
    <div v-transfer-dom>
      <popup v-model="classValue" width="100%">
        <ztree
          :data="data"
          @nodeClick="nodeClick"
          :name="'departName'"

        ></ztree>
      </popup>
    </div>
  </div>
</template>
<script>
  import ztree from "@commonComponents/tree"
  import formatDate from "../../utils/formatDate"
  import VueBetterScroll from "vue2-better-scroll"
  import {XHeader, Tabbar, TabbarItem, Group, Cell, TransferDom, Popup} from 'vux'

  export default {
    data() {
      return {
        /*下拉刷新*/
        resultlist: [],
        openRightIndex:-1,
        scrollbarObj: {fade: true},
        pullDownRefreshObj: false,
        pullUpLoadObj: {
          threshold: 0,
          txt: {
            more: "加载更多",
            noMore: "没有更多数据了"
          }
        },
        startY: 0,
        scrollToX: 0,
        scrollToY: 0,
        scrollToTime: 700,
        isDisabled: true, // 是否禁止输入

        detailData: {
          DeptName: ''
        },
        params: {
          startDate: '',
          endDate: '',
          // parentId:0,
          // status: 0,
          skipCount: 0,
          maxResultCount: 10,
          rocordNotPlan: 1
          // rocordNotPlan:0

        },
        resultTotal:{},
        startDate: '',
        endDate: '',
        tableType: [{
          quantizeName: "",
          id: "",
          children: {
            quantizeName: "",
            id: "",
          }
        }],
        result: [{
          checkDepartId: "",
          checkDepartName: "",
          charge: "",
          classifyOneId: "",
          classifyOneName: "",
          classifyTwoId: "",
          classifyTwoName: "",
          checkDate: "",
          checkGroupLeaderId: "",
          checkGroupLeaderName: "",
          receiverId: "",
          receivrtName: "",
          checkNo: "",
          status: "",
          approvalResult: "",
          approvalRemark: "",
          approver: "",
          approverId: "",
          approvalDate: "",
          rocordId: "",
          rocordNotPlan: "",
          rocordStatus: "",
          checkScore: "",
          deleteTag: "",
          deleteUser: "",
          deleteDate: "",
          beDepartId: "",
          beDepartName: "",
          divisionList: "",
          allowApproval: "",
          id: ""
        }],
        flag: false,
        classValue: false,
        data: [
        ]
      }
    },
    components: {
      ztree, Popup, VueBetterScroll
    },
    directives: {
      TransferDom
    },
    created() {
      this.startDate = new Date().getFullYear() + '-01-01'
      this.endDate = formatDate()
      this.getAssessmentPlan()
      this.getTableTypeList()
      this.getTotal()
    },
    mounted(){
        let that = this;
        this.$bridge.registerHandler("setToken", function(data) {
                         that.$store.state.Authorization =
          "Bearer " + data;
         that.getAssessmentPlan()
         that.getTableTypeList()
         that.getTotal()
            //这里data ioS返回的是token值(字符串)
        });

        this.connectWebViewJavascriptBridge(function(bridge) {
            bridge.init(function(message, responseCallback) {});

            bridge.registerHandler("setToken", function(data) {
                             that.$store.state.Authorization =
          "Bearer " + data;
          that.getAssessmentPlan()
          that.getTableTypeList()
          that.getTotal()
                  //这里data android返回的是token值(字符串)

            });
        });
    },
    methods: {
      connectWebViewJavascriptBridge(callback) {
            if (window.WebViewJavascriptBridge) {
                callback(WebViewJavascriptBridge);
            } else {
                document.addEventListener(
                "WebViewJavascriptBridgeReady",
                function() {
                    callback(WebViewJavascriptBridge);
                },
                false
                );
            }
        },
      nextStep(item) {
         if((item.status ==0 || item.status ==3) && item.allowUpdate==1){
           this.$router.push({path:'./addPlan',query:{id:item.id}})
         }else{
           this.$router.push({path:'./planDetails',query:{planId:item.id,allowApproval:item.allowApproval}})
         }
      },
      search() {
        if (this.startDate) {
          this.params.startDate = this.startDate + " 00:00:00";
        }else{
          this.params.startDate =this.startDate
        }
        if (this.endDate) {
          this.params.endDate = this.endDate + " 00:00:00";
        }else{
          this.params.endDate = this.endDate
        }
        this.resultlist = [];
        this.getAssessmentPlan();
        this.getTotal();
      },
      onPullingUp() {

        let limitcount = this.resultlist.length;
        this.params.skipCount = limitcount;
        this.getAssessmentPlan();
      },
      goBack() {
        this.$bridge.callHandler({
              type: 'closePage',
              data: {}
            });
      },
      gotoAddPlan(type) {
        this.$store.state.tableTypeName = type
        this.$router.push({path: './addPlan', query: {id: ''}})
      },
      getCompany() {
        this.examinedUnits()
      },
      nodeClick(item) {
        if (item.departType == 6 || item.departType == 7 || item.departType == 8) {
          this.detailData.beDepartName = item.departName
          this.params.checkDepartId = item.id
          this.classValue = false
        } else {
          alert("只能选择项目部")
          /*this.$vux.toast.text("只能选择项目部")*/
        }
      },
      getTableTypeList() {
        this.$axiosAjax.getTableTypeList({parentId: "0"}).then((res) => {
          if (res.data.result.isSuccess == true) {
            this.tableType = res.data.result.data
          }
        }).catch(
          (err) => {

          }
        )

      },
      getTotal() {
        if(this.$route.query.status!=null) {
            this.params.status = this.$route.query.status
            // this.params.isGetAll=0
        }
        this.$axiosAjax.ClassAssessmentTotal(this.params).then((res) => {
          if (res.data.success == true) {
            this.resultTotal = res.data.result
          }
        }).catch(
          (err) => {

          }
        )

      },
      getAssessmentPlan() {
        if(this.$route.query.status!=null) {
            this.params.status = this.$route.query.status
            // this.params.isGetAll=0
        } else {
            // this.params.isGetAll=1
        }
        this.$axiosAjax.AppGetPageListAll(this.params).then((res) => {
          if (res.data.success == true) {
            let relist = res.data.result.items;
            if (this.params.skipCount == 0) {
              this.resultlist = []
            }
            this.resultlist = this.resultlist.concat(relist);
            this.resultlist = Object.assign([], this.resultlist)
            let totalAllCount = res.data.result.totalCount;
            let resultCount = this.resultlist.length;
            if (parseInt(resultCount) < parseInt(totalAllCount)) {
              this.$refs['scrollqq'].forceUpdate(true);
            } else {
              this.$refs['scrollqq'].forceUpdate(false);
            }
          }
        }).catch(
          (err) => {

          }
        )
      },
      clearStartDate() {
        this.startDate = "";
      },
      clearEndDate() {
        this.endDate = "";
      },
      examinedUnits() {
        //debugger;//单位
        this.$axiosAjax.examinedUnits({parentId: this.$store.state.departId}).then((res) => {
          if (res.data.success == true) {
            this.classValue = true;
            this.data = res.data.result;
            console.log(this.data);
          }
        }).catch(
          (err) => {

          }
        )

      },
      onSwipeLeft(index){
        this.openRightIndex=index
      },
      //删除
      del(item,index){
        // if(item.status!=0){
        //   this.$vux.toast.text("已提交无法删除")
        //   return;
        // }
        if (item.status!=0) {
          if(this.$store.state.useraccount!='admin'){
            this.$vux.toast.text("无删除权限")
            return;
          }
        }
        this.openRightIndex=-1
        var ids=[]
        if(item.allowUpdate!=1){
          this.$vux.toast.text("无删除权限")
          return;
        }
        ids.push(item.id)
        this.$axiosAjax.deletePlanbyId({id: ids}).then((res) => {
          if (res.data.success == true) {
            this.$vux.toast.text("删除成功")
            this.resultlist.splice(index,1)
            this.getAssessmentPlan()
            this.getTotal()
          }
        }).catch(
          (err) => {

          }
        )
      },
    },
    watch: {
      "params.startTime": function () {
        // this.getAssessmentPlan()
      },
      "params.endTime": function () {
        //this.getAssessmentPlan()
      },
      "params.checkDepartId": function () {
        //this.getAssessmentPlan()
      }
    }
  }
</script>
<style lang="less" scoped>
  .search-btn {
    width: 200px;
    line-height: 36px;
    border-radius: 20px;
    margin: 23px auto;
    color: #fff;
    background: #469b7a;
    text-align: center;
    font-size:16px;
    letter-spacing: 4px;
  }

  .form-item {
    .input_div {
      .vux-x-icon {
        fill: #999;
        float: right;
        margin-top: 18px;
      }
    }
  }

  .project_div {
    .project_list {
      padding-left: 20px;
      li {
        border-bottom: 1px solid #efeff4;
        height: 105px;
        position: relative;
        padding-top: 18px;
        padding-right: 20px;
        h4 {
          width: calc(100% - 40px);
          text-overflow: ellipsis;
          white-space: nowrap;
          overflow: hidden;
          font-size: 16px;
          margin-bottom: 8px;
        }
        p {
          span {
            font-size: 14px;
            color: #666;
          }
          span:first-child {
            margin-right: 40px;
          }
        }
        .project_status {
          margin-top: 14px;
          strong {
            font-size: 12px;
          }
          strong.project_status1 {
            color: #ff7333;
          }
          strong.project_status2 {
            color: #469b7a;
          }
        }
        .vux-x-icon {
          fill: #999;
          right: 20px;
          top: 50%;
          margin-top: -11px;
          position: absolute;
        }
      }
    }
  }

  .add_btn {
    position: fixed;
    background: url('../../assets/imgs/add_pro_icon.png') no-repeat;
    background-size: 62px;
    height: 62px;
    width: 62px;
    right: 30px;
    bottom: 35px;
    z-index: 1;
  }

  .addPlan_select {
    position: fixed;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, .5);
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 10;
    .planSel_list {
      width: 60%;
      margin: 0 auto;
      padding-top: 150px;
      li {
        border-radius: 10px;
        height: 57px;
        line-height: 57px;
        text-align: center;
        background: #fff;
        margin-bottom: 6px;
        color: #469b7a;
        font-size: 16px;
      }
    }
  }
</style>
